package com.baosight.sczx.common.zm.cInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;

/**
 * 组织机构接口
 * <p>Title: Department.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2010-12-16 下午02:16:11  严洪浩  Create
 *</pre>
 */
public class Department {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	private static Department instance = new Department();
	
	@SuppressWarnings("rawtypes")
	private static List<Map> _list;
	
	private Department(){
		initData();//当第一次使用的时候初始化
	}
	
	/**
	 * 初始化数据
	 * <p>Function: initData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-9上午05:41:20 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initData() {
		if(_list != null) {
			_list = null;
		}
		_list = new ArrayList<Map>();
		try {
			String miningCode = UserSession.getInSessionProperty("miningCode") == null ? "C.102" : UserSession.getInSessionProperty("miningCode").toString().trim();
			
			Map<String, String> _map = new HashMap<String, String>();
			
			_map.put("levelOneName", "C."+miningCode.substring(2, 5));
			_map.put("levelTwoName", "D."+miningCode.substring(2, 5));
			
			
			// 查询总记录行数  （因为iplat4j平台的查询每次最多只查询1000条记录，因此如果要一次性加载出1000条记录以上需要循环查询）
		    int totalRecord = Integer.parseInt(dao.query("ZM02.queryInterfaceCount", _map).get(0).toString());
		    int num = totalRecord % 1000 == 0 ? (totalRecord / 1000) : (totalRecord / 1000 + 1);

		    List<Map> tempList = null;
		    int offset = 0; // 从第几条记录开始查询
		    int limit = 1000; // 平台最大1000条（每次最多查询100条）
		    for (int i = 0; i < num; i++) {
		      tempList = new ArrayList<Map>();
		      tempList = dao.query("ZM02.queryInterface", _map, offset, limit);
		      _list.addAll(tempList);
		      if (i != num - 1) {
		        offset += limit;
		      }
		    }
			
		} catch (Exception e) {
			System.out.println(Department.class.getName() + "获取所有部门异常：" + e.getMessage());
		}
	}
	
	/**
	 * 材料费用额度管理树-获取所有生产部门
	 * <p>Function: getDepartment</p>
	 * <p>Description: </p>
	 * <p>return value:List</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21 14:36:17 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> getDepartmentForTree(){                                                              
		List<Map> _list = new ArrayList<Map>();
		try {
			_list = dao.query("ZM02.queryTreeNodes", null);
		} catch (Exception e) {

		}
		return _list;
	}
	
	
	/**
	 * 根据ID获取部门，返回Map
	 * <p>Function: getDepartmentMapById</p>
	 * <p>Description: </p>
	 * <p>return value:Map</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-16下午02:16:30 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public Map getDepartmentMapById(String orgCode) {
		for(int i = 0; i < _list.size(); i++) {
			Map _map = _list.get(i);
			
			String key = _map.get("orgCode") == null ? "" : _map.get("orgCode").toString();
			if(key.equals(orgCode)) {
				return _map;
			}
			
		}
		return new HashMap();
	}
	
	/**
	 * 根据ID获得部门名称
	 * <p>Function: getNameById</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-16下午02:27:59 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public String getDepartmentNameById(String orgCode) {
		for(int i = 0; i < _list.size(); i++) {
			Map _map = _list.get(i);
			
			String key = _map.get("orgCode") == null ? "" : _map.get("orgCode").toString();
			if(key.equals(orgCode)) {
				return _map.get("orgName").toString();
			}
		}
		
		return null;
	}
	
	/**
	 * 构造部门Block供其他模块使用
	 * <p>Function: getConstructionUnit</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-16上午08:13:49 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiBlock getConstructionUnit(String _blockID, String _value, String _display, boolean isAll) {
		EiBlock block = new EiBlock(_blockID);

		EiColumn eiColumn = new EiColumn(_value);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn(_display);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		List<Map> _result = new ArrayList<Map>();
		if(isAll)  {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put(_value, "");
			_map.put(_display, "");
			_result.add(_map);
			
			for(int i = 0; i < _list.size(); i++) {
				_result.add(_list.get(i));
			}
		}
		else {
			_result = _list;
		}
		
		block.setRows(_result);
		return block;
	}
	
	/**
	 * 构造部门Block供其他模块使用
	 * <p>Function: getConstructionUnit</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-16上午08:13:46 严洪浩
	 * </p>
	 *
	 */
	public EiBlock getConstructionUnit(boolean isAll) {
		return this.getConstructionUnit("cmes.cs.constructionUnit", "orgCode", "orgName", isAll);
	}
	
	/**
	 * 获取机构类型是区队的部门（也是施工部门）
	 * <p>Function: getOutputDepts</p>
	 * <p>Description: type的值包含采煤，掘进，其他</p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-8-27上午08:31:59 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> getOutputDepts(String type){
		List<Map> result = new ArrayList<Map>();
		for(int i=0;i<_list.size();i++){
			if("区队".equals(_list.get(i).get("property")) && type.equals(_list.get(i).get("ext2"))){
				result.add(_list.get(i));
			}
		}
		return result;
	}
	
	/**
	 * 获取各矿的部门
	 * <p>Function: getOutputDepts</p>
	 * <p>Description: miningId的值为矿ID</p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-3-24上午08:31:59 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiBlock getMiningDepts(String _blockID, String _value, String _display, boolean isAll, String parentName){
		EiBlock block = new EiBlock(_blockID);

		EiColumn eiColumn = new EiColumn(_value);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn(_display);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		List<Map> _result = new ArrayList<Map>();
		if(isAll)  {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put(_value, "");
			_map.put(_display, "");
			_result.add(_map);
		}
		
		for(int i = 0; i < _list.size(); i++) {
			if( parentName.equals(_list.get(i).get("parentName"))){
				_result.add(_list.get(i));
			}
		}
		
		block.setRows(_result);
		return block;
	}
	
	/**
	 * 初始化该类
	 * <p>Function: getInstance</p>
	 * <p>Description: </p>
	 * <p>return value:InitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-7上午07:55:35 严洪浩
	 * </p>
	 *
	 */
	public static Department getInstance(){
		//第一次访问时status可能为0也可能为1
		if(CmesConstants.DEPARTMENT_STATUS == 1) {
			instance = new Department();
		}
		return instance;
	}
}
